/**
 * Created by Administrator on 2017/3/12 0012.
 */
import { Injectable } from '@angular/core';
import { API_URL } from '../app/config';
import { AlertController } from 'ionic-angular';
import 'rxjs/add/operator/toPromise';
import { Http, Response, Headers, RequestOptions } from '@angular/http';
// import { Http, Response, RequestOptions } from '@angular/http';

/*
 Generated class for the SignIn page.

 See http://ionicframework.com/docs/v2/components/#navigation for more info on
 Ionic pages and navigation.
 */
@Injectable()
export class HttpServies {
    constructor(
        private http: Http,
        private alertCtrl:AlertController
    ){
    }
    private handleError(error: Response) {
        console.log(error);
    }
    private svrUrl(svrName: string): string {
        return `${API_URL}${svrName}&`;
    }
    httpPost(url,body:any=""){
        var headers = new Headers();
        //headers.set('Content-Type', 'application/json;charset=utf-8');
        headers.set('Content-Type', 'application/x-www-form-urlencoded');
        headers.set('token', localStorage.getItem('token'));
        let options = new RequestOptions({ headers: headers });
        return this.http.post(this.svrUrl(url),body, options).toPromise()
            .then(res => res.json())
            .catch(err => {
                return this.handleError(err);
            });
    }
    showErro(err){
        let alert = this.alertCtrl.create({
            title: '提示',
            subTitle: err,
            buttons: ['OK']
        });
        alert.present();
    }
    isLogin(){
        if(localStorage.getItem('token')){
            return true;
        }else{
            return false;
        }
    }
}